package com.example.homework;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceControl;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Fragment message=new MessageFragment();
    private Fragment contact=new ContactFragment();
    private Fragment find=new FindFragment();
    private Fragment me=new MeFragment();
    private FragmentManager fm=getSupportFragmentManager();//定义Manager

    private FragmentTransaction transaction=fm.beginTransaction();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化Fragment
        initFragment();

        //点击事件
        //clickEvent();

        View main=findViewById(R.id.MainView);
        View l1=findViewById(R.id.Llayout1);
        View l2=findViewById(R.id.Llayout2);
        View l3=findViewById(R.id.Llayout3);
        View l4=findViewById(R.id.Llayout4);

        l1.setOnClickListener(this);
        l2.setOnClickListener(this);
        l3.setOnClickListener(this);
        l4.setOnClickListener(this);

        onClick(main);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Llayout1:
                show(message);
                break;
            case R.id.Llayout2:
                show(contact);
                break;
            case R.id.Llayout3:
                show(find);
                break;
            case R.id.Llayout4:
                show(me);
                break;
        }
    }

    //显示Fragment
    public void show(Fragment fragment){
        hideFrafment();
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.show(fragment).commit();
    }
    //初始化Fragment
    public void initFragment(){
        transaction
                .add(R.id.content_frame,message)
                .add(R.id.content_frame,contact)
                .add(R.id.content_frame,find)
                .add(R.id.content_frame,me);
        hideFrafment();
        //默认显示页面
        transaction
                .show(message)
                .commit();

    }
    //隐藏所有Fragment
    public void hideFrafment(){
        FragmentTransaction transaction=fm.beginTransaction();
        transaction
                .hide(message)
                .hide(contact)
                .hide(find)
                .hide(me)
                .commit();
    }

    /*
    //点击事件函数
    public void clickEvent(){

        View button1 = findViewById(R.id.Llayout1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction=fm.beginTransaction();

                Log.d("button1", "这是消息按钮");
                hideFrafment();
                transaction.show(message).commit();
            }
        });

        View button2=findViewById(R.id.Llayout4);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction=fm.beginTransaction();
                Log.d("button2", "这是我按钮");
                hideFrafment();
                transaction.show(me).commit();
            }
        });

        View button3=findViewById(R.id.Llayout2);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction=fm.beginTransaction();
                Log.d("button3", "这是通讯录按钮");
                hideFrafment();
                transaction.show(contact).commit();
            }
        });

        View button4=findViewById(R.id.Llayout3);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction=fm.beginTransaction();
                Log.d("button4", "这是发现按钮");
                hideFrafment();
                transaction.show(find).commit();
            }
        });

    }
    */

}